package com.xhlj.security.handler;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhlj.entity.UserInfo;
import com.xhlj.service.UserInfoService;
import com.xhlj.utils.JwtHelper;
import com.xhlj.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class CustomUsernamePasswordAuthenticationSuccessHandler implements AuthenticationSuccessHandler {



    @Resource
    private JwtHelper jwtHelper;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

//        if (authentication instanceof UsernamePasswordAuthenticationToken) {
//            String username = authentication.getName();
//            LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(UserInfo::getUserPhone, username);
//
//            UserInfo user = userInfoService.getOne(wrapper);
//            String token = jwtHelper.createToken((long) user.getUserId());
//            Map res = new HashMap();
//            res.put("username", username);
//            res.put("token", token);
//
//            response.setContentType("application/json;charset=UTF-8");
//            response.getWriter().print(JSON.toJSONString(Result.build(res, 200, "login success")));
//        }

    }
}
